﻿using System;
using System.IO;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using PlugNT.Common;
using PlugNT.Custom;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Article
{
    public partial class Admin : AdminPage
    {
        private Article curArticle = new Article();
        private Templet curTemplet = new Templet();
        private int pageSize = 15;
        private string rootpath = "";

        public string currpostfix = "";
        public string clslist = "";
        public string keyword = "";
        public string isexact = "";
        public string showsql = "";

        private const string bindtag = "bindtag";
        public string jsonString = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            rootpath = this.Server.MapPath("/");
            currpostfix = (Request.QueryString["currpostfix"] == null) ? WebConfig.TablePostfix : Request.QueryString["currpostfix"].Trim();
            curArticle.CurPostfix = currpostfix;
            if (!IsPostBack)
            {
                try
                {
                    clslist = (Request.QueryString["clslist"] == null) ? "" : Request.QueryString["clslist"].Trim();
                    keyword = (Request.QueryString["keyword"]==null)?"":Request.QueryString["keyword"].Trim();
                    isexact = (Request.QueryString["isexact"] == null) ? "0" : Request.QueryString["isexact"].Trim();
                    showsql = (Request.QueryString["showsql"] == null) ? "0" : Request.QueryString["showsql"].Trim();
                    
                    //绑定列表选项
                    DdlMenu_bind();
                    switch (Request["t"])
                    {
                        case "delete":
                            Delete_news(Convert.ToInt32(Request["id"]));
                            break;
                        case "commend":
                            if (Request["commend"] == "1") { Commend_news(Convert.ToInt32(Request["id"]), false); }
                            else { Commend_news(Convert.ToInt32(Request["id"]), true); }
                            break;
                        case "stick":
                            if (Request["stick"] == "1") { Stick_news(Convert.ToInt32(Request["id"]), false); }
                            else { Stick_news(Convert.ToInt32(Request["id"]), true); }
                            break;
                    }
                    Repeater_admin_bind(clslist, keyword);

                }
                catch { Response.Redirect("../Message.aspx?message=数据绑定错误！&z=no"); }
            }
        }

        private void Repeater_admin_bind(string tclslist, string keyword)
        {
            string gopage = (Request.QueryString["page"] == null) ? "0" : Request.QueryString["page"].Trim();
            int page = Convert.ToInt32(gopage);

            bool tmp_isExact=(isexact == "1");
            bool tmp_isShowSql=( showsql == "1");
            int recordCount = 0; 

            DataTable dt = curArticle.GetList(pageSize, (page - 1),
                tclslist, keyword, tmp_isExact, tmp_isShowSql, out recordCount);

            litPager.Text = Pager.Pagination(recordCount, pageSize, page, "Admin.aspx", GetBindUrl(), "", true, true);

            Repeater_admin.DataSource = dt.DefaultView;
            Repeater_admin.DataBind();

        }


        #region 类别数据绑定
        private void DdlMenu_bind()
        {

            //分表
            string seleClslist = "";
            DataTable dt2 = new Tables().GetList();
            this.ddlTables.Items.Clear();
            foreach (DataRow dr in dt2.Rows)
            {
                string description = dr["description"].ToString().Trim();
                string postfix = dr["postfix"].ToString().Trim();
                ListItem li = new ListItem(description, postfix);
                this.ddlTables.Items.Add(li);
                if (currpostfix.ToString() == postfix)
                {
                    seleClslist = dr["bindclslist"].ToString().Trim();
                }
            }
            this.ddlTables.SelectedValue = currpostfix.ToString();
            this.ddlTables.Attributes.Add("onchange", "SubmitUrl();");



            //栏目
            DataTable dt = new Channel().GetOrderList(seleClslist);
            this.ddlMenu.Items.Clear();
            this.ddlMenu.Items.Add(new ListItem("所有栏目", ""));
            foreach (DataRow dr in dt.Rows)
            {
                int ClassTj = Convert.ToInt32(dr["clslistlen"]);
                string tmpClslist = dr["clslist"].ToString().Trim();
                string showfile = dr["showfile"].ToString().Trim();
                string ClassName = dr["clsname"].ToString().Trim();
                if (ClassTj != 1)
                {
                    ClassName = "├ " + ClassName;
                    ClassName = PlugNT.Common.Utils.StringOfChar(ClassTj - 1, "　") + ClassName;
                }
                ListItem li = new ListItem(ClassName, tmpClslist);
                if (showfile == "")
                {
                    li.Attributes.Add("style", "background-color:#ffdfe2;");
                }
                this.ddlMenu.Items.Add(li);
            }
            this.ddlMenu.SelectedValue = clslist;

        }
        #endregion

        //删除一条数据
        private bool Delete_news(int id)
        {
            ArticleInfo model=curArticle.GetArticleInfo(id);
            if (model != null)
            {
                DataRow dr = curArticle.GetPreviousDataRow(model.id, model.clsno);
                if (dr != null)
                {
                    string hurl = (dr["jumpurl"].ToString() != "") ? dr["jumpurl"].ToString() : dr["createpath"].ToString() + dr["pagename"].ToString() + ".shtml";
                    model.previouspage = "<a id=\"pntPrevious" + model.id.ToString() + "\" href=\"" + hurl + "\">" + dr["title"].ToString() + "</a>";
                }
                dr = curArticle.GetNextDataRow(model.id, model.clsno);
                if (dr != null)
                {
                    string hurl = (dr["jumpurl"].ToString() != "") ? dr["jumpurl"].ToString() : dr["createpath"].ToString() + dr["pagename"].ToString() + ".shtml";
                    model.nextpage = "<a id=\"pntNext" + model.id.ToString() + "\" href=\"" + hurl + "\">" + dr["title"].ToString() + "</a>";
                }
                if (curArticle.UpdatePart(model))
                {
                    return curArticle.Delete(id);
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        //发布
        private bool Publish_news(int id, bool isPublish)
        {
            return curArticle.Publish(id, isPublish);
        }

        //推荐
        private bool Commend_news(int id, bool commend)
        {
            return curArticle.Commend(id, commend);
        }

        //置顶
        private bool Stick_news(int id, bool stick)
        {
            return curArticle.Stick(id, stick);
        }

        private string GetBindUrl()
        {
            return "currpostfix="+currpostfix.ToString()+"&clslist=" + clslist + "&keyword=" + keyword + "&isexact=" + isexact + "&showsql=" + showsql + "&page";
        }

        //判断是否存在文件
        protected void Repeater_admin_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal litHasFile = (Literal)e.Item.FindControl("litHasFile");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            string filepath = drv["createpath"].ToString() + drv["pagename"].ToString() + ".shtml";

            if (File.Exists(rootpath + filepath))
            {
                litHasFile.Text = "<img src=\"../images/file.gif\" align=\"absmiddle\" title=\"已生成文件\" />";
            }
            else
            {
                litHasFile.Text = "<img src=\"../images/err.gif\" align=\"absmiddle\" title=\"未生成文件\" />";
            }
        }

        //批量删除
        protected void ImageButton_all_del_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    if (Delete_news(id))
                    {
                        nub += 1;
                    }
                }
            }
            Response.Redirect("../Message.aspx?message=批量删除<b>" + nub + "</b>条信息成功！&z=yes");
        }

        //批量推荐
        protected void ImageButton_tj_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    if (Commend_news(id, true))
                    {
                        nub += 1;
                    }
                }
            }
            Response.Redirect("../Message.aspx?message=批量推荐<b>" + nub + "</b>条信息成功！&z=yes");
        }

        //批量置顶
        protected void ImageButton_ding_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            int nub = 0;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                if (CK.Checked)
                {
                    if(Stick_news(id, true))
                    {
                        nub += 1;
                    }
                }
            }
            Response.Redirect("../Message.aspx?message=批量置顶<b>" + nub + "</b>条信息成功！&z=yes");

        }

        //批量发布
        protected void ImageButton_K_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

            int nub = 0;
            int nup = 0;
            HtmlBuilder html = new HtmlBuilder();
            html.TablePostFix = currpostfix;
            for (int i = 0; i < Repeater_admin.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)Repeater_admin.Items[i].FindControl("Label_id")).Text);
                CheckBox CK = (CheckBox)Repeater_admin.Items[i].FindControl("ID_list");
                HiddenField hidClsno = (HiddenField)Repeater_admin.Items[i].FindControl("hidClsno");
                HiddenField hidCreatepath = (HiddenField)Repeater_admin.Items[i].FindControl("hidCreatepath");
                HiddenField hidPagename = (HiddenField)Repeater_admin.Items[i].FindControl("hidPagename");
                if (CK.Checked)
                {
                    //发布
                    if (Publish_news(id, true))
                    {
                        nup++;
                    }
                    //生成
                    string clsno = hidClsno.Value;
                    DataTable dtTemplet = curTemplet.GetListByClsno(clsno);
                    if (dtTemplet.Rows.Count > 0)
                    {
                        DataRow drTemplet = dtTemplet.Rows[0];
                        string error = PageBuilder.CreateShowHtml(html, id.ToString(), hidPagename.Value.Trim(), hidCreatepath.Value.Trim(), drTemplet["showfile"].ToString(), clsno);
                        if (error == "")
                        {
                            nub += 1;
                        }
                    }
                }
            }
            Response.Redirect("../Message.aspx?message=已发布<b>" + nup + "</b>条信息成功，<b>已生成" + nub + "</b>条信息成功！" + "&z=yes");
        }
    }
}
